#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-09-27 22:28:27
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

import os
import MySQLdb
from sqlalchemy import create_engine,Table,MetaData,Column,Integer,String,tuple_
from sqlalchemy.sql import select, asc, and_

db_info = {
    'host' : "127.0.0.1",
    'port' : 3306,
    'user' : 'root',
    'passwd' : 'root',
    'db' : 'aops',
    'charset' : 'utf8'
    }

DB_URI = 'mysql://root:root@127.0.0.1/jfpython'

# try:
#     con = MySQLdb.connect(db_info['host'],db_info['user'],db_info['passwd'],db_info['db'])
#     cur = con.cursor()
#     cur.execute('SELECT VERSION()')
#     print cur.fetchone()
# except Exception, e:
#     raise e

eng = create_engine(DB_URI)

meta = MetaData(eng)
users = Table(
    'Users',meta,
    Column('id',Integer,primary_key=True,autoincrement=True),
    Column('name',String(50),nullable=False),
    )
if users.exists():
    users.drop()

def execute(s):
    print '#'*20
    rs = con.execute(s)
    for row in rs:
        print row['id'],row['name']

def eng.connect() as con:
    for username in ('aa','bb','cc'):
        user = users.insert().values(name=username)
        con.execute(user)

